#include "PicModuleState.h"
#include "MainLoopState.h"
#include "Portfolio.h"
#include <fstream>
#include <sstream>

PicModuleState::PicModuleState(string name) :ModuleState(name), picnum(0), current_pic(0)
{
}

PicModuleState::~PicModuleState(void)
{
}

void PicModuleState::enter()
{

	state = 2;
	//Reset Mouse Wheel
	position_mouse_z(0);
	scare_mouse();

	//Generate Text for Game
	dataFileName = "PORT/PIC/" + name + "/" + name + "_G.dat";

	//Load Graphic file for the game
	port->getRMgr()->loadDataFile(dataFileName);

	//Read help text file
	helpFileName = "PORT/PIC/" + name + "/DOC/" + name + ".txt";
	char bufin[512];
	line = 0;
	fstream openf;
	openf.open(helpFileName.c_str(),ios::in);
	while (!openf.eof())
	{
		openf.getline(bufin, 256);
		strcpy(helpText[line++], bufin);
	}
	for(int i = 3; i < 256; i++)
	{
		helpText[0][i - 3] = helpText[0][i];
	}
	openf.close();
	openf.clear();

	//BEGIN READING PICTURE DESCRIPTIONS
	string picFileName = "PORT/PIC/" + name + "/DESC.txt";
	openf.open(picFileName.c_str(), ios::in);
	//The first line desc.txt give how many pictures
	openf.getline(bufin, 256);
	for(int i = 3; i < 256; i++)
	{
		bufin[i-3] = bufin[i];
	}

	picnum = atoi(bufin);
	////Then grab the descriptions
	//int l = 0;
	//while (!openf.eof() )
	//{
	//	strcpy(picDesc[l], bufin);
	//	strcpy(empty_string, bufin);
	//	openf.getline(bufin, 50);
	//	strcpy(picDesc[l++], bufin);
	//}
	openf.close();
	openf.clear();

	submenubutton_pmask = create_allegro_pmask(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenubuttonpmask].dat);

	//Mouse hit detection image
	B_mousehit = create_bitmap(1,1);
	mousehit_pmask = create_allegro_pmask(B_mousehit);

	B_Reverse = create_bitmap(64,64);
	ASSERT(B_Reverse);
	clear_to_color(
		B_Reverse,
		makecol(255,255,0));
	draw_sprite_h_flip(
		B_Reverse,
		(BITMAP *)port->getRMgr()->GraphicPackA[Nextpic].dat,
		0,0);
	//flipped pmask
	nextpic_f_pmask = create_allegro_pmask(
		B_Reverse);
	ASSERT(nextpic_f_pmask);
	//normal
	nextpic_pmask   = create_allegro_pmask(
		(BITMAP *)port->getRMgr()->GraphicPackA[Nextpic].dat);
	ASSERT(nextpic_pmask);
}


void PicModuleState::leave()
{
	destroy_pmask(mousehit_pmask);
	destroy_pmask(submenubutton_pmask);

	destroy_pmask(nextpic_f_pmask);
	destroy_pmask(nextpic_pmask);

	release_bitmap(B_mousehit);
	release_bitmap(B_Reverse);

	port->getRMgr()->unloadDataFile(dataFileName);
	clear_keybuf();

	port->getGUtil()->clearBuffers();
}

void PicModuleState::update(const float delta)
{
	checkMouse();
	if(state == 2)
	{
		if(check_pmask_collision(nextpic_pmask, mousehit_pmask,PicButXA,PicButY,mouse_x,mouse_y))
		{
			if(mouse_b == 1 && mouselock == 0)
			{
				mouselock = 1;
				current_pic--;
				if(current_pic < 0)
				{
					current_pic = picnum - 1;
				}
			}
		}
		if(check_pmask_collision(nextpic_f_pmask, mousehit_pmask,PicButXB,PicButY,mouse_x,mouse_y))
		{
			if(mouse_b == 1 && mouselock == 0)
			{
				mouselock = 1;
				current_pic = (current_pic + 1) % picnum;
			}
		}
	}

	// If we are quiting save the back buffer for the fade out
	if (quit)
	{
		port->changeState(new MainLoopState());
	}
}

void PicModuleState::render()
{
	port->getGUtil()->drawToBackBuffer(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenu].dat,
		0, 0);

	drawButtons(3);

	if(state == 2)
	{
		rectfill(
			port->getGUtil()->getLevel(0),
			52,12,
			52 + 700,12 + 30,
			makecol(0,0,0));
		rectfill(
			port->getGUtil()->getLevel(0),
			50,10,
			50 + 700,10 + 30,
			makecol(0,0,255));

		BITMAP *temp = (BITMAP *)port->getRMgr()->getData(current_pic, dataFileName);

		port->getGUtil()->drawToBackBuffer(
			(BITMAP *)port->getRMgr()->getData(current_pic, dataFileName),
			(SCREEN_W - (temp->w))/2,
			(SCREEN_H - (temp->h))/2);

		//port->getGUtil()->drawTextToBackBuffer(
		//	port->getRMgr()->font,
		//	picDesc[current_pic],
		//	52,12,
		//	makecol(255,255,255));

		//Draw left right arrows
			port->getGUtil()->drawAlphaToBackBuffer(
			B_Reverse,
			PicButXA,
			PicButY);

		port->getGUtil()->drawAlphaToBackBuffer(
			(BITMAP *)port->getRMgr()->GraphicPackA[Nextpic].dat,
			PicButXB,
			PicButY);
	}

	moduleTextHelp(buttonroll);

	//Mouse pointer dot
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 3,
		mouse_y - 3,
		mouse_x + 3,
		mouse_y + 3,
		makecol(0,0,0));
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 2,
		mouse_y - 2,
		mouse_x + 2,
		mouse_y + 2,
		makecol(255,255,255));

	stringstream ss;
	string buf;
	ss<<picnum;
	ss>>buf;
	port->getGUtil()->drawTextToBackBuffer(
		port->getRMgr()->font,
		buf.c_str(),
		20,20,
		makecol(255,0,0));

	ss<<current_pic;
	ss>>buf;
	port->getGUtil()->drawTextToBackBuffer(
		port->getRMgr()->font,
		buf.c_str(),
		20,20,
		makecol(255,0,0));

	port->getGUtil()->drawBackBufferToScreen();
	port->getGUtil()->clearBuffers();
}